//![[CC
import { Align } from "../GMDBase/TypeDef";
import mainLoop from "../GMDBase/mainLoop";
//!]]
/*[*/export default/*]*/ class UCBanner
{
    static get I():UCBanner 
    {
        if(!this.g_I) this.g_I = new UCBanner()
        return this.g_I
    }
    static g_I:UCBanner

    Go()
    {
        //主循环
        mainLoop.startLoop(()=>{
            //console.log("ucbn loop")
            if(this.mNeedReload)
            {
                console.log("ucbn mNeedReload=true")
                var now = (new Date()).getTime()
                if(now - this.mNeedReloadTime<this.mDelayTime) 
                { 
                    console.log("ucbn reload cd")
                    return
                }
                this.mNeedReload = false
                this.mDelayTime*=2
                this.reLoad()
            }//else
            //console.log("ucbn mNeedReload=false")
            return true
        })

        this.reLoad()
    }


    reLoad()
    {
        if(!this.checkVersion()) return
        if(this.mLoading) 
        { 
            console.log("ucbn reload ret")
            return
        }
        console.log("ucbn reload")
        this.mLoadSuccess = false

        var scsize = cc.view.getVisibleSizeInPixel()
        var ratio = cc.view.getDevicePixelRatio()

        this.m_hAlign = Align.Center
        this.m_vAlign = Align.End 
 

        this.m_boxw = scsize.width/ratio 

        if(this.m_BannerAd) {
            this.m_BannerAd.destroy()
            this.m_BannerAd = undefined
        }
        
        var style:any = {
           width:this.m_boxw,
           gravity:7
        }

        if(cc.sys.os==cc.sys.OS_IOS)
        {
            style.height = 220
        }

        console.log("ucbn screen wh:",scsize.width,scsize.height)
        console.log("ucbn create banner style:",style)
        this.m_BannerAd = uc.createBannerAd(
            {
                //posId:gcfg.BannderUnitId,
                style:style
            }
        )

        this.mLoading = true

        this.m_BannerAd.onLoad(()=>{
            console.log("ucbn Banner onLoad")
            this.mNeedReload = false
            this.mLoadSuccess = true 
            this.mDelayTime = 10000
            this.mLoading = false
        })

        this.m_BannerAd.onError((res)=>{
            console.error("ucbn Banner onError",res)
            this.mLoading = false
            this.SetNeedReload()
        })

    }
    public show(
        obj:{
            //包围盒定义
            left:number, top:number, width:number, height:number,

            //水平对齐方式 垂直对齐方式
            hAlign:Align,vAlign:Align,

            //广告位置id
            channel:number,

            fail:Function
        }
        )
    {
        if(!this.checkVersion()) return
 

        if(!this.mLoadSuccess)
        {
            if(obj.fail) obj.fail()
            this.SetNeedReload() 
            return
        }

        this.mLoadSuccess = false
        this.mShowing = true

        console.log("ucbn banner show")
        this.m_BannerAd.show().then(()=>{
            console.log("ucbn banner 展示成功")
            
        }).catch((err)=>{
            console.log('ucbn banner广告展示失败', JSON.stringify(err));
            this.SetNeedReload()
        })
    }

    public hide()
    {
        if(!this.checkVersion()||!this.m_BannerAd) return
        console.log("ucbn hide")

        if(this.mShowing)
        { 
            console.log("ucbn hide#1")
            this.mShowing = false
            this.m_BannerAd.hide()
        }
        
        if(!this.mLoadSuccess)
        {
            console.log("ucbn set need reload")
            this.SetNeedReload()
        }
    }


    SetNeedReload()
    {
        if(this.mNeedReload||this.mLoading) return
        this.mNeedReload = true
        this.mNeedReloadTime = (new Date()).getTime()
        console.log("ucbn banner 请求重装")
    }

    checkVersion():boolean
    {
        if(!window["uc"]||!uc.createBannerAd)
        {
            console.error("ucbn 当前sdk不支持banner!")
            return false
        }

        return true
    }


    m_IsVisible:boolean
    m_BannerAd:any
    m_hAlign:Align
    m_vAlign:Align
    m_Channel:number
     
    m_boxw:number 

    mLoadSuccess:boolean = false 
    mNeedReload:boolean = false
    mNeedReloadTime:number//需要重新装载时的时间
    mDelayTime:number = 10000
    mShowing:boolean = false
    mLoading:boolean = false
} 